/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Dimensions, Platform } from 'react-native';
import Animated, { Easing, ReduceMotion } from 'react-native-reanimated';

const { height: WINDOW_HEIGHT, width: WINDOW_WIDTH } = Dimensions.get('window');
const { height: SCREEN_HEIGHT, width: SCREEN_WIDTH } = Dimensions.get('screen');

/**
 * sheet展示位置枚举
 * 定义了不同类型的表单展示方式
 */
enum SHEET_TYPE {
  AUTO,
  BOTTOM,
  CENTER,
  SIDE,
}

// 手势拖拽来源枚举
enum GESTURE_SOURCE {
  UNDETERMINED = 0,
  SCROLLABLE, // 可滚动内容手势
  HANDLE, // 拖拽句柄手势
  CONTENT, // 内容区域手势
}

enum SHEET_STATE {
  CLOSED = 0,
  OPENED,
  EXTENDED,
  OVER_EXTENDED,
  FILL_PARENT,
}

enum SCROLLABLE_STATE {
  LOCKED = 0,
  UNLOCKED,
  UNDETERMINED,
}

enum SCROLLABLE_TYPE {
  UNDETERMINED = 0,
  VIEW,
  FLATLIST,
  SCROLLVIEW,
  SECTIONLIST,
  VIRTUALIZEDLIST,
}

enum ANIMATION_STATE {
  UNDETERMINED = 0,
  RUNNING,
  STOPPED,
  INTERRUPTED,
}

enum ANIMATION_SOURCE {
  NONE = 0,
  MOUNT,
  GESTURE,
  USER,
  CONTAINER_RESIZE,
  SNAP_POINT_CHANGE,
}

enum ANIMATION_METHOD {
  TIMING,
  SPRING,
}

const ANIMATION_EASING: Animated.EasingFunction = Easing.out(Easing.exp); // 缓动函数
const ANIMATION_DURATION = 250; // 动画持续时间

const ANIMATION_CONFIGS_IOS = {
  // // 弹簧物理属性
  damping: 500, // 阻尼
  stiffness: 1000, // 刚度
  mass: 3, // 质量
  overshootClamping: true, // 过冲限制
  restDisplacementThreshold: 10, // 静止位移阈值
  restSpeedThreshold: 10, // 静止速度阈值
  ...(ReduceMotion ? { reduceMotion: ReduceMotion.Never } : {}),
};

const ANIMATION_CONFIGS_ANDROID = {
  duration: ANIMATION_DURATION, // 动画持续时间
  easing: ANIMATION_EASING, // 动画缓动函数
  ...(ReduceMotion ? { reduceMotion: ReduceMotion.Never } : {}), // 是否减少动画
};

const ANIMATION_CONFIGS = Platform.OS === 'ios' ? ANIMATION_CONFIGS_IOS : ANIMATION_CONFIGS_ANDROID;

const SCROLLABLE_DECELERATION_RATE_MAPPER = {
  [SCROLLABLE_STATE.UNDETERMINED]: 0,
  [SCROLLABLE_STATE.LOCKED]: 0,
  [SCROLLABLE_STATE.UNLOCKED]: Platform.select({
    ios: 0.998,
    android: 0.985,
    default: 1,
  }),
};

const MODAL_STACK_BEHAVIOR = {
  replace: 'replace',
  push: 'push',
};

export {
  SHEET_TYPE,
  GESTURE_SOURCE,
  SHEET_STATE,
  ANIMATION_STATE,
  ANIMATION_METHOD,
  ANIMATION_SOURCE,
  SCROLLABLE_TYPE,
  SCROLLABLE_STATE,
  WINDOW_HEIGHT,
  WINDOW_WIDTH,
  SCREEN_HEIGHT,
  SCREEN_WIDTH,
  SCROLLABLE_DECELERATION_RATE_MAPPER,
  MODAL_STACK_BEHAVIOR,
  ANIMATION_CONFIGS,
  ANIMATION_EASING,
  ANIMATION_DURATION,
};
